<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Address extends Base_Controller {

    public function index() {
        $this->load->view('public/header');
        $this->load->view('pages/addsender');
    }

    public function group() {


        $this->load->view('public/header');
        $this->load->view('pages/group');
    }

    public function senderlist() {
        $where = [
            'department_id' => $this->session->department_id
        ];
        $this->data['sumRecords'] = $this->address_model->getSum('su_sender', $where);
        $this->load->view('public/header', $this->data);
        $this->load->view('pages/senderlist');
    }

    public function addreceiver() {
        $where = [
            'department_id' => $this->session->department_id
        ];
        $this->data['group'] = $this->common_model->getList('su_group', $where);
        $this->load->view('public/header', $this->data);
        $this->load->view('pages/addreceiver');
    }

    public function receiverlist() {
        $where = [
            'department_id' => $this->session->department_id
        ];
        $this->data['sumRecords'] = $this->address_model->getSum('su_receiver', $where);
        //$this->data['group'] = $this->common_model->getList('su_group',$where);
        $this->load->view('public/header', $this->data);
        $this->load->view('pages/receiverlist');
    }

    public function addgroup() {
        $redata = $this->httpcode;
        $name = $this->input->post('type');
        $brief = $this->input->post('type');
        $sort = $this->input->post('sort');
        $department_id = $this->session->department_id;
        $uid = $this->session->id;
        $data = [
            'name' => $name,
            'brief' => $brief,
            'create_time' => tnow(),
            'sort' => $sort,
            'department_id' => $department_id,
            'uid' => $uid
        ];
        $res = $this->common_model->addItem('su_group', $data);
        if ($res) {
            $redata['errno'] = 666;
            $redata['error'] = '分组添加成功';
        } else {
            $redata['errno'] = 600;
            $redata['error'] = '分组添加失败';
        }
        echo json_encode($redata);
    }

    public function get_group_list() {
        $redata = $this->httpcode;
        $department_id = $this->session->department_id;
        $where = [
            'department_id' => $department_id
        ];
        $res = $this->common_model->getList('su_group', $where);
        if (!empty($res)) {
            $redata['errno'] = 666;
            $redata['error'] = '获取分组成功';
            $redata['datalist'] = $res;
        } else {
            $redata['errno'] = 600;
            $redata['error'] = '分组数据为空';
        }
        echo json_encode($redata);
    }

    public function del_group() {
        $redata = $this->httpcode;
        $id = $this->input->post('tid');
        $where = [
            'id' => $id
        ];
        $res = $this->common_model->delItem('su_group', $where);
        if ($res) {
            $redata['errno'] = 666;
            $redata['error'] = '删除分组成功';
            $redata['datalist'] = $res;
        } else {
            $redata['errno'] = 600;
            $redata['error'] = '删除分组失败';
        }
        echo json_encode($redata);
    }

    public function edit_group() {
        $redata = $this->httpcode;
        $type = $this->input->post('type');
        $sort = $this->input->post('sort');
        $brief = $this->input->post('brief');
        $id = $this->input->post('tid');

        $data = [
            'name' => $type,
            'sort' => $sort,
            'brief' => $brief
        ];
        $where = [
            'id' => $id
        ];
        $res = $this->common_model->updateItem('su_group', $where, $data);
        if ($res) {
            $redata['errno'] = 666;
            $redata['error'] = '修改分组信息成功';
            $redata['datalist'] = $res;
        } else {
            $redata['errno'] = 600;
            $redata['error'] = '修改分组信息失败';
        }
        echo json_encode($redata);
    }

    /*
     * 获取数据库中的联系人数据
     */

    public function get_address_list() {
        $redata = $this->httpcode;
        $type = $this->input->get('type');
        $page = $this->input->get('page');
        $limit = $this->input->get('limit');
        $page = $page * $limit;
        if ($type == 1) {
            $table = 'su_sender';
            $msg = '发件人';
        } else {
            $table = 'su_receiver';
            $msg = '收件人';
        }
        $department_id = $this->session->department_id;


        if ($type == 1) {
            $where = [
                'department_id' => $department_id
            ];
            $addresslist = $this->address_model->addressInfo($table, $where, $page, $limit);
        } else {
            $where = [
                'su_receiver.department_id' => $department_id
            ];
            $addresslist = $this->address_model->receiveraddressInfo($where, $page, $limit);
        }


        if (!empty($addresslist)) {
            $redata['errno'] = 666;
            $redata['error'] = $msg . '列表获取成功';
            $redata['addresslist'] = $addresslist;
        } else {
            $redata['errno'] = 600;
            $redata['error'] = '暂时还没有添加任何信息';
        }
        echo json_encode($redata);
    }

    public function del_address() {
        $redata = $this->httpcode;
        $type = $this->input->post('type');
        $where = $this->input->post('ids');
        if ($type == 1) {
            $table = 'su_sender';
            $msg = '发件人';
        } else {
            $table = 'su_receiver';
            $msg = '收件人';
        }
        $state = $this->address_model->delAddress($table, $where);
        if ($state) {
            $redata['errno'] = 666;
            $redata['error'] = $msg . '数据删除成功';
        } else {
            $redata['errno'] = 600;
            $redata['error'] = $msg . '数据删除失败';
        }
        echo json_encode($redata);
    }

    public function update_address() {
        $redata = $this->httpcode;
        $type = $this->input->post('type');
        $ids = $this->input->post('ids');
        $enable = $this->input->post('enable');
        $where = [
            'id' => $ids
        ];
        $data = [
            'enable' => $enable
        ];
        if ($type == 1) {
            $table = 'su_sender';
            $msg = '发件人';
        } else {
            $table = 'su_receiver';
            $msg = '收件人';
        }
        $state = $this->address_model->updateAddress($table, $where, $data);
        if ($state) {
            $redata['errno'] = 666;
            $redata['error'] = $msg . '信息修改成功';
        } else {
            $redata['errno'] = 600;
            $redata['error'] = $msg . '信息修改成功';
        }
        echo json_encode($redata);
    }

}
